package com.efounder.form.util;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.efounder.form.comp.FormScrollView;
import com.efounder.form.comp.IUIComponent;
import com.efounder.form.comp.VWrapGroup;

/**
 * @author lch
 * 主要抽取组件一些共有的方法。
 */

public class FormCompUtil {

    /**
     * 设置view的宽、高、gap
     */
    public static void setLayoutSize(View view) {
        if (!(view.getParent() instanceof IUIComponent)) return;
        //设置控件宽高
        IUIComponent parent = (IUIComponent) view.getParent();
        if (parent != null) {
            int parentWidth = parent.getViewSize().getWidth();
            int parentHeight = parent.getViewSize().getHeight();

            if (!(view instanceof IUIComponent)) {
                throw new RuntimeException("当前组件没有继承IUIComponent，请联系组件开发者。");
            } else {
                int layoutWidth = parentWidth * ((IUIComponent) view).getPercentWidth() / 100;
                int layoutHeight = parentHeight * ((IUIComponent) view).getPercentHeight() / 100;

                layoutWidth = layoutWidth == 0 ? ((IUIComponent) view).getCompWidth() : layoutWidth;
                layoutHeight = layoutHeight == 0 ? ((IUIComponent) view).getCompHeight() : layoutHeight;

                ((IUIComponent) view).getViewSize().setWidth(layoutWidth);
                ((IUIComponent) view).getViewSize().setHeight(layoutHeight);
                ViewGroup.LayoutParams layoutParams;
                if (parent instanceof FrameLayout) {
                    layoutParams = new FrameLayout.LayoutParams(layoutWidth, layoutHeight);
                } else {
                    layoutParams = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
                    ((LinearLayout.LayoutParams) layoutParams).rightMargin = parent.getGap();
                    ((LinearLayout.LayoutParams) layoutParams).bottomMargin = parent.getGap();
                }

                if (parent instanceof VWrapGroup || parent instanceof FormScrollView) {
                    //自适应高度的组件不要设置百分比
                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams
                            (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    view.setLayoutParams(layoutParams1);
                } else {
                    view.setLayoutParams(layoutParams);

                }
            }
        } else {
            int layoutWidth = ((IUIComponent) view).getCompWidth();
            int layoutHeight = ((IUIComponent) view).getCompHeight();

            ((IUIComponent) view).getViewSize().setWidth(layoutWidth);
            ((IUIComponent) view).getViewSize().setHeight(layoutHeight);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
            if (parent instanceof VWrapGroup || parent instanceof FormScrollView) {
               //自适应高度的组件不要设置百分比
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams
                        (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(layoutParams1);

            } else {
                view.setLayoutParams(layoutParams);

            }
        }
    }

    /**
     * @param horizontalAlign
     * @param verticalAlign
     * @return
     */
    public static int getLayoutGravity(String horizontalAlign, String verticalAlign) {
        int hAlign;
        int vAlign;

        if (horizontalAlign == null) {
            hAlign = Gravity.LEFT;
        } else if (horizontalAlign.equals("left")) {
            hAlign = Gravity.LEFT;
        } else if (horizontalAlign.equals("right")) {
            hAlign = Gravity.RIGHT;
        } else {
            hAlign = Gravity.CENTER_HORIZONTAL;
        }

        if (verticalAlign == null) {
            vAlign = Gravity.TOP;
        } else if (verticalAlign.equals("top")) {
            vAlign = Gravity.TOP;
        } else if (verticalAlign.equals("middle")) {
            vAlign = Gravity.CENTER_VERTICAL;
        } else {
            vAlign = Gravity.BOTTOM;
        }
        //this.setGravity(hAlign | vAlign);
        return hAlign | vAlign;
    }
}